package com.chinafree.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.chinafree.auth.model.enumeration.Column;
import com.chinafree.business.mapping.UserMapping;
import com.chinafree.business.model.result.BaseUserResult;
import com.chinafree.business.model.result.ChatResult;
import com.chinafree.business.model.result.CompanyResult;
import com.chinafree.business.service.ChatService;
import com.chinafree.common.model.enumeration.ResponseCodeEnum;
import com.chinafree.common.model.response.ListPagingResponse;
import com.chinafree.common.model.response.ListResponse;
import com.chinafree.mapper.ChUserCompanyMapper;
import com.chinafree.mapper.MaCompanyMapper;
import com.chinafree.mapper.MaUserMapper;
import com.chinafree.model.po.ChUserCompany;
import com.chinafree.model.po.MaCompany;
import com.chinafree.model.po.MaUser;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ChatServiceImpl extends BaseServiceImpl implements ChatService {

    @Autowired
    private ChUserCompanyMapper chUserCompanyMapper;

    @Autowired
    private MaCompanyMapper maCompanyMapper;

    @Autowired
    private MaUserMapper maUserMapper;

    private void getCompanyList() {
        chUserCompanyMapper.selectCompany();
    }

    

    @Override
    public ListResponse<CompanyResult> getCompany() {
        Long userId = getUserId();
        List<Long> companyIdList = chUserCompanyMapper.getUserList(userId);
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            List<MaCompany> list = maCompanyMapper.selectBatchIds(companyIdList);
            return new ListResponse(ResponseCodeEnum.SUCCESS, "成功", UserMapping.instance.maCompanyToResults(list));
        }
        return new ListResponse(ResponseCodeEnum.SUCCESS, "成功", new ArrayList());
    }

    @Override
    public ListResponse<BaseUserResult> getUser() {
        Long companyId = getCompanyId();
        List<Long> userList = chUserCompanyMapper.getCompanyList(companyId);
        if (CollectionUtils.isNotEmpty(userList)) {
            List<MaUser> maUserList = maUserMapper.selectBatchIds(userList);
            List<BaseUserResult> list = UserMapping.instance.maUserToBaseUserResults(maUserList);
            return new ListResponse(ResponseCodeEnum.SUCCESS, "成功", list);
        }
        return new ListResponse(ResponseCodeEnum.SUCCESS, "成功", new ArrayList<>());
    }

    public void insert(ChUserCompany chUserCompany) {
        chUserCompanyMapper.insert(chUserCompany);
    }

    @Override
    public ListResponse<ChatResult> pullMsg(Long uid, Long companyId) {
        // TODO Auto-generated method stub
        Map<String, Object> map = new HashMap<>();
        map.put(Column.COMPANY_ID.getColumn(), companyId);
        map.put(Column.USER_ID.getColumn(), uid);
        List<ChUserCompany> list = chUserCompanyMapper.selectByMap(map);
        List<ChatResult> list1 = list.stream().map(this::getChatResult).collect(Collectors.toList());

        return new ListResponse<>(ResponseCodeEnum.SUCCESS,"success",list1);
    }


    private ChatResult getChatResult(ChUserCompany c) {
        return ChatResult.builder().roleId(c.getFromUser()).message(c.getMessage()).time(c.getCreateTime()).build();
    }

}
